import {
    MessageBox,
    Message
} from 'element-ui';
import apiBase from "@/api/apiBase.js";
import { ORDER } from "@/const/orderConst.js";
var baseUrl;
var socket;
var imgPaths = "";
var imgs = [];

export default {
    //连接
    connects(message) {
        console.log('我是连接')
        baseUrl = "ws://127.0.0.1:12345";
        apiBase.DoService(
            {},
            apiBase.getApiAdress().Param.GetHighmeter,
            res => {
                let data = res.data.content;
                if (data == 10) {
                    openSocket()
                }else if(data == 40){
                    openSocketNew()
                }
            }
        );

    },
}

function openSocket() {
    socket = new WebSocket(baseUrl);
    socket.onclose = function () {
        console.error("web channel closed");
    };
    socket.onerror = function (error) {
        console.error("web channel error: " + error);
    };
    socket.onopen = function () {
        new QWebChannel(socket, function (channel) {
            //获取c++注册对象
            window.dialog = channel.objects.dialog;
            //网页关闭函数
            window.onunload = function () {
                dialog.get_actionType("closeSignal");
            }

            //网页控件事件，设备列表点击
            //devChanged信号，输入参数可以为设备名，出图模式，分辨率，切换前先关闭视频传输
            document.getElementById("devLists").onchange = function () {
                dialog.get_actionType("closeVideo");
                var select = document.getElementById("devLists");
                dialog.devChanged(select.value);//打开一个预览窗口
            };


            //分辨率列表点击，切换前先关闭视频传输
            document.getElementById("resolutionLists").onchange = function () {
                dialog.get_actionType("closeVideo");
                var select = document.getElementById("resolutionLists");
                dialog.devChanged(select.value);
            };

            //打开视频按钮点击(判断打开一个，还是多个视频窗口，需要两种html文件一种就是现在这样的切换设备)
            document.getElementById("openVideos").onclick = function () {
                console.log(5656);
                dialog.get_actionType("closeVideo");
                var select = document.getElementById("devLists");
                dialog.devChanged(select.value);
                //dialog.get_actionType("openVideo");
            };

            //提交上传
            document.getElementById("uploadss").onclick = function () {
                console.log('我要上传！！');
                for (var i = 0; i < imgs.length; i++) {
                    CreateFile(imgs[i]);
                }
            };


            //关闭视频按钮点击
            document.getElementById("closeVideos").onclick = function () {
                removeAll();
            };
            //取消按钮，关闭视频
            document.getElementById("closeDialogs").onclick = function () {
                // dialog.get_actionType("closeSignal");//如果使用这个的话，再次点击打开视频的时候无效
                dialog.get_actionType("closeVideo");
            };

            //拍照按钮点击
            document.getElementById("photographs").onclick = function () {
                dialog.photoBtnClicked("single");
                dialog.get_actionType("savePhoto");
            };

            //清空缩略图
            document.getElementById("FileUploads").onclick = function () {
                removeAll();
            };

            //接收设备名(在设备列表中添加或删除item)
            dialog.send_devName.connect(function (message) {
                console.log('我是设备名');
                //判断是否存在，否 加入，是 删除
                var select = document.getElementById("devLists");
                if (message.indexOf('delete') >= 0) {
                    var msg = message.substr(6);
                    // output("delete devs: " + msg);
                    select.remove(msg);
                }
                else {
                    // output("Received devs: " + message);
                    //副头放在列表末
                    if (message.indexOf("USB") >= 0) {
                        select.add(new Option(message));
                    } else {
                        select.add(new Option(message), 0);
                        select.selectedIndex = 0;
                    }
                }
            });

            //接收设备分辨率，若为空，则清空现有列表
            dialog.send_resolutionList.connect(function (message) {
                var select = document.getElementById("resolutionLists");
                console.log('设备分辨率')
                if (message) {
                    // output("Received resolution: " + message);
                    select.add(new Option(message));
                } else {
                    select.options.length = 0;
                }
                if (select.options.length > 3) {
                    select.selectedIndex = 2;
                }
            });

            //服务器返回消息
            dialog.sendPrintInfo.connect(function (message) {
                // output(message);
                //图片保存后返回路径，根据文件路径增加缩略图
                //字符串格式savePhoto_success:C:/Users/Administrator/Desktop/eloamPhoto/2018.06.25.19.32.31.jpg
                if (message.indexOf("savePhoto_success:") >= 0) {
                    // imgPath = message.substr(18);
                    // addImgDiv();
                }

            });
            //视频
            dialog.send_imgData2.connect(function (message) {
                changeImgdata(message);
            });
            //接收图片流用来展示，单个，较大的base64
            dialog.send_imaData3.connect(function (message) {
                changeImgdata_paizhaos(message);
                // photographData = message;						
            });
            //网页加载完成信号
            dialog.html_loaded("one");
        });
    }
    // }
}

function openSocketNew() {
    socket = new WebSocket(baseUrl);
    socket.onclose = function () {
        console.error("web channel closed");
    };
    socket.onerror = function (error) {
        console.error("web channel error: " + error);
    };
    socket.onopen = function () {
        new QWebChannel(socket, function (channel) {
            //获取c++注册对象
            window.dialog = channel.objects.dialog;
            //网页关闭函数
            window.onunload = function () {
                dialog.get_actionType("closeSignal");
            }

            //网页控件事件，设备列表点击
            //devChanged信号，输入参数可以为设备名，出图模式，分辨率，切换前先关闭视频传输
            document.getElementById("devLists").onchange = function () {
                var select = document.getElementById("devLists");
                dialog.devChanged("primaryDev_:" + select.value);//打开一个预览窗口
            };


            //分辨率列表点击，切换前先关闭视频传输
            document.getElementById("resolutionLists").onchange = function () {
                var select = document.getElementById("resolutionLists");
                dialog.devChanged("primaryDev_:" + select.value);
            };

            //打开视频按钮点击(判断打开一个，还是多个视频窗口，需要两种html文件一种就是现在这样的切换设备)
            document.getElementById("openVideos").onclick = function () {
                var resolutionList = document.getElementById("resolutionLists");
                resolutionList.options.length = 0;
                var select = document.getElementById("devLists");
                console.log(select.value)
                dialog.devChanged("primaryDev_:" + select.value);
            };

            //提交上传
            document.getElementById("uploadss").onclick = function () {
                console.log('我要上传！！');
                for (var i = 0; i < imgs.length; i++) {
                    CreateFile(imgs[i]);
                }
            };


            //关闭视频按钮点击
            document.getElementById("closeVideos").onclick = function () {
                removeAll();
            };
            //取消按钮，关闭视频
            document.getElementById("closeDialogs").onclick = function () {
                dialog.get_actionType("closePriVideo");
            };

            //拍照按钮点击
            document.getElementById("photographs").onclick = function () {
                dialog.photoBtnClicked("primaryDev_");
				dialog.get_actionType("savePhotoPriDev");
            };

            //清空缩略图
            document.getElementById("FileUploads").onclick = function () {
                removeAll();
            };

            //服务器返回消息
            dialog.sendPrintInfo.connect(function (message) {
                /********主头设备信息***********/
                //设备名
                if (message.indexOf("priDevName:") >= 0) {
                    message = message.substr(11);
                    var select = document.getElementById("devLists");
                    select.add(new Option(message));
                    select.selectedIndex = 1;
                }
                //主头设备分辨率
                else if (message.indexOf("priResolution:") >= 0) {
                    message = message.substr(14);
                    var select = document.getElementById("resolutionLists");
                    select.add(new Option(message));
                    if (select.options.length > 3) {
                        select.selectedIndex = 2;
                    }
                }

            });
            //视频
            dialog.send_priImgData.connect(function (message) {
                changeImgdata(message);
            });
            //接收图片流用来展示，单个，较大的base64
            dialog.send_priPhotoData.connect(function (message) {
                changeImgdata_paizhaos(message);
                // photographData = message;						
            });
            //网页加载完成信号
            dialog.html_loaded("one");
        });
    }
    // }
}

//根据文本换图片
function changeImgdata(message) {
    var element = document.getElementById('bigs');
    element.src = "data:image/jpg;base64," + message;
}


//拍照时用来展示的img
function changeImgdata_paizhaos(message) {
    imgPaths = "data:image/jpg;base64," + message;
    addImgDiv();
    imgs.push(message);
}
//附件上传
function CreateFile(val) {
    let data = {
        FRCMBDID: ORDER.FRCMBDID,
        FRCCODNAME: ORDER.FCMFCSID,
        FileContentBase64: val,
        FleEnt: '.jpg',
    };
    apiBase.DoService(
        data,
        apiBase.getApiAdress().FileConfig.CreateFile,
        res => {
            Message.success('上传成功！')
            if (res.data.msg == '成功') {
                Message.success('上传成功！')
                removeAll();
            } else {
                Message.error('上传失败！')
            }
        }
    );
}

//增加图片缩略图
function addImgDiv() {
    var container = document.getElementById('containers');
    var newchild = document.createElement("div");
    newchild.setAttribute("style", "float:left;margin-top:10px;margin-left:10px;");
    newchild.setAttribute("id", imgPaths);
    newchild.innerHTML = "<img width='100' height='90' src='" + imgPaths + "' onclick='openImage(this)'/></img>";
    container.appendChild(newchild);
}
//清空缩略图
function removeAll() {
    document.getElementById('containers').innerHTML = "";
    imgs = [];
    var element = document.getElementById('bigs');
    element.src = null;
}
